/**
 * Panel cho phep nguoi dung dang nhap hoac dang ky
 */
package view;
import java.awt.Color;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.TitledBorder;

import model.Constant;
import model.database.UserDB;
import model.entity.User;
import control.MD5Service;
import control.PlayingService;

/**
 * @author heroandtn3
 * @date Oct 22, 2012
 */
public class LoginPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static final String KEY = "LoginPanel";
	private JPanel loginPanel;
	private JLabel lbLoginNotice;
	private JLabel lbReg;
	private JLabel lbBack;
	private JLabel lbBg;
	private JLabel lbLoginReset;
	private JLabel lbRegReset;
	private JLabel lbRegName;
	private JLabel lbRegPass;
	private JPasswordField tfRegPass;
	private JTextField tfRegName;
	private JLabel lbRegNotice;
	private JPanel regPanel;
	private JLabel lbNameLogin;
	private JLabel lbPassLogin;
	private JLabel lbSignin;
	private JTextField tfLoginName;
	private JPasswordField tfLoginPass;
	private MainPanel mainPanel;

	/**
	 * 
	 */
	public LoginPanel(MainPanel mainPanel) {
		// TODO Auto-generated constructor stub
		this.mainPanel = mainPanel;
		initGUI();
	}

	private void initGUI() {
		try {
			{
				this.setPreferredSize(new java.awt.Dimension(900, 680));
				this.setLayout(null);
				this.setBackground(new java.awt.Color(0,138,0));
				{
					loginPanel = new JPanel();
					loginPanel.setBackground(new java.awt.Color(0,138,0));
					this.add(loginPanel);
					loginPanel.setBorder(BorderFactory.createTitledBorder(null, "Sign in", TitledBorder.LEADING, TitledBorder.DEFAULT_POSITION, null, new java.awt.Color(255,255,255)));
					loginPanel.setLayout(null);
					loginPanel.setBounds(486, 30, 378, 264);
					{
						lbLoginNotice = new JLabel();
					}
					{
						tfLoginPass = new JPasswordField();
						tfLoginPass.setBorder(new SoftBevelBorder(BevelBorder.LOWERED,null,null,null,null));
						tfLoginPass.setBounds(153, 88, 167, 21);
						tfLoginPass.addMouseListener(new MouseAdapter() {
							public void mouseClicked(MouseEvent evt) {
								//TODO add your code for tfLoginPass.mouseClicked
								lbLoginNotice.setText("");
							}
						});
					}
					{
						tfLoginName = new JTextField();
						tfLoginName.setBorder(new SoftBevelBorder(BevelBorder.LOWERED,null,null,null,null));
						tfLoginName.addMouseListener(new MouseAdapter() {
							public void mouseClicked(MouseEvent evt) {
								//TODO add your code for tfLoginName.mouseClicked
								lbLoginNotice.setText("");
							}
						});
					}
					{
						lbSignin = new MyLabelButton(Constant.BUTTON_DIR + "ok", "Click to login", false);
						loginPanel.add(lbSignin);
						loginPanel.add(tfLoginPass);
						tfLoginPass.addKeyListener(new KeyAdapter() {
							public void keyPressed(KeyEvent evt) {
								//TODO add your code for tfLoginPass.keyPressed
								if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
									String name = tfLoginName.getText();
									@SuppressWarnings("deprecation")
									String pass = tfLoginPass.getText();
									if (name.length() <= 0 || pass.length() <= 0) {
										lbLoginNotice.setForeground(new java.awt.Color(255,0,0));
										lbLoginNotice.setText("Some required fields is missing!");
									} else {
										UserDB uDB = new UserDB();
										User u = null;
										u = uDB.getUserByName(name);
										if (u != null && (MD5Service.md5From(pass)).equals(u.getPassword())) {
											
											mainPanel.getUserHomePanel().setUser(u);
											mainPanel.getUserHomePanel().initListMap();
											mainPanel.getUserHomePanel().initHighScore();
											mainPanel.switchPanel(UserHomePanel.KEY);
											PlayingService playSv = new PlayingService();
											playSv.setUser(u);
											mainPanel.getUserHomePanel().setPlaySv(playSv);
										
										} else {
											lbLoginNotice.setForeground(new java.awt.Color(255,0,0));
											lbLoginNotice.setText("Login failed!");
										}
									}
								
								}
							}
						});
						loginPanel.add(tfLoginName);
						tfLoginName.setBounds(153, 40, 167, 21);
						
						lbSignin.setBounds(272, 127, 48, 48);
						lbSignin.addMouseListener(new MouseAdapter() {
							public void mouseClicked(MouseEvent evt) {
								// TODO add your code for lbSignin.mouseClicked
								String name = tfLoginName.getText();
								@SuppressWarnings("deprecation")
								String pass = tfLoginPass.getText();
								if (name.length() <= 0 || pass.length() <= 0) {
									lbLoginNotice.setForeground(new java.awt.Color(255,0,0));
									lbLoginNotice.setText("Some required fields is missing!");
								} else {
									UserDB uDB = new UserDB();
									User u = null;
									u = uDB.getUserByName(name);
									if (u != null && (MD5Service.md5From(pass)).equals(u.getPassword())) {
										mainPanel.getUserHomePanel().setUser(u);
										mainPanel.getUserHomePanel().initListMap();
										mainPanel.getUserHomePanel().initHighScore();
										mainPanel.switchPanel(UserHomePanel.KEY);
										PlayingService playSv = new PlayingService();
										playSv.setUser(u);
										mainPanel.getUserHomePanel().setPlaySv(playSv);
									
									} else {
										lbLoginNotice.setForeground(new java.awt.Color(255,0,0));
										lbLoginNotice.setText("Login failed!");
									}
								}
							}
						});
					}
					{
						lbLoginReset = new MyLabelButton(Constant.BUTTON_DIR + "reset", "Reset", false);
						loginPanel.add(lbLoginReset);
						lbLoginReset.setBounds(153, 127, 48, 48);
						lbLoginReset.addMouseListener(new MouseAdapter() {
							public void mouseClicked(MouseEvent evt) {
								//TODO add your code for lbLoginReset.mouseClicked
								tfLoginName.setText("");
										tfLoginPass.setText("");
							}
						});
					}
					{
						lbPassLogin = new JLabel();
						loginPanel.add(lbPassLogin);
						lbPassLogin.setText("Password");
						lbPassLogin.setBounds(17, 91, 91, 14);
						lbPassLogin.setForeground(new java.awt.Color(255,255,255));
						lbPassLogin.setFont(new java.awt.Font("Segoe UI",0,16));
					}
					{
						lbNameLogin = new JLabel();
						loginPanel.add(lbNameLogin);
						loginPanel.add(lbLoginNotice);
						lbLoginNotice.setBounds(17, 203, 344, 49);
						lbNameLogin.setText("Name");
						lbNameLogin.setBounds(17, 43, 91, 14);
						lbNameLogin.setForeground(new java.awt.Color(255,255,255));
						lbNameLogin.setFont(new java.awt.Font("Segoe UI",0,16));
					}
				}
				{
					regPanel = new JPanel();
					regPanel.setBackground(new java.awt.Color(0,138,0));
					this.add(regPanel);
					regPanel.setBorder(BorderFactory.createTitledBorder(null, "Register", TitledBorder.LEADING, TitledBorder.DEFAULT_POSITION, null, new java.awt.Color(255,255,255)));
					regPanel.setLayout(null);
					regPanel.setBounds(486, 329, 378, 264);
					{
						lbRegNotice = new JLabel();
					}
					{
						lbReg = new MyLabelButton(Constant.BUTTON_DIR + "ok", "Click to register", false);
						regPanel.add(lbReg);
						lbReg.setBounds(284, 142, 48, 48);
						lbReg.addMouseListener(new MouseAdapter() {
							public void mouseClicked(MouseEvent evt) {
								// TODO add your code for lbSignin.mouseClicked
								String name = tfRegName.getText();
								String pass = new String(tfRegPass.getPassword());
								if (name.length() <= 0 || pass.length() <= 0) {
									lbRegNotice.setForeground(new java.awt.Color(255,0,0));
									lbRegNotice.setText("Some required fields is missing!");
								} else if (name.length() > 45) {
									lbRegNotice.setForeground(new java.awt.Color(255,0,0));
									lbRegNotice.setText("Name is too long!");
								} else {
									UserDB uDB = new UserDB();
									User u = new User(name, MD5Service.md5From(pass));
									if (uDB.insertUser(u)) {
										lbRegNotice.setForeground(Color.GREEN);
										lbRegNotice.setText("Successful registration!");
									} else {
										lbRegNotice.setForeground(new java.awt.Color(255,0,0));
										lbRegNotice.setText("Registration failure! The name has been used!");
									}
								}
								
								
							}
						});
					}
					{
						lbRegReset = new MyLabelButton(Constant.BUTTON_DIR + "reset", "Reset", false);
						regPanel.add(lbRegReset);
						lbRegReset.setBounds(165, 142, 48, 48);
						lbRegReset.addMouseListener(new MouseAdapter() {
							public void mouseClicked(MouseEvent evt) {
								//TODO add your code for lbRegReset.mouseClicked
								tfRegName.setText("");
										tfRegPass.setText("");
							}
						});
					}
					{
						tfRegName = new JTextField();
						regPanel.add(tfRegName);
						tfRegName.setBorder(new SoftBevelBorder(BevelBorder.LOWERED,null,null,null,null));
						tfRegName.setBounds(165, 50, 167, 21);
						tfRegName.addMouseListener(new MouseAdapter() {
							public void mouseClicked(MouseEvent evt) {
								//TODO add your code for tfRegName.mouseClicked
								lbRegNotice.setText("");
							}
						});
					}
					{
						tfRegPass = new JPasswordField();
						regPanel.add(tfRegPass);
						tfRegPass.setBorder(new SoftBevelBorder(BevelBorder.LOWERED,null,null,null,null));
						tfRegPass.setBounds(165, 104, 167, 21);
						tfRegPass.addMouseListener(new MouseAdapter() {
							public void mouseClicked(MouseEvent evt) {
								//TODO add your code for tfRegPass.mouseClicked
								lbRegNotice.setText("");
							}
						});
					}
					{
						lbRegPass = new JLabel();
						regPanel.add(lbRegPass);
						lbRegPass.setText("Password");
						lbRegPass.setBounds(17, 107, 91, 14);
						lbRegPass.setForeground(new java.awt.Color(255,255,255));
						lbRegPass.setFont(new java.awt.Font("Segoe UI",0,16));
					}
					{
						lbRegName = new JLabel();
						regPanel.add(lbRegName);
						regPanel.add(lbRegNotice);
						lbRegNotice.setBounds(17, 202, 344, 50);
						lbRegName.setText("Name");
						lbRegName.setBounds(17, 53, 91, 14);
						lbRegName.setForeground(new java.awt.Color(255,255,255));
						lbRegName.setFont(new java.awt.Font("Segoe UI",0,16));
					}
				}
				{
					lbBack = new MyLabelButton(Constant.BUTTON_DIR + "back", "Back to home", false);
					this.add(lbBack);
					lbBack.setBounds(655, 609, 48, 48);
					lbBack.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent evt) {
							//TODO add your code for lbBack.mouseClicked
							mainPanel.switchPanel("HomePanel");
						}
					});
				}
				{
					lbBg = new JLabel(new ImageIcon(Constant.BG_DIR + "play.png"));
					this.add(lbBg);
					lbBg.setBounds(125, 197, 256, 256);
				}
			}
		} catch (Exception e) {
			//e.printStackTrace();
		}
	}

}
